package social.server.main;

import java.util.List;
import java.util.Random;
/**
 * 
 * Classe que representa as requisicoes de amizade entre menbros da rede
 * Social
 * 
 * @author Caio
 *
 */

public class Request {
	private String requestingUser;
	private String destinationUser;
	private int codeRequest;
	
	
	public Request(String requestingUser, String destinationUser){
		this.requestingUser = requestingUser;
		this.destinationUser = destinationUser;
		generateCode();
		
	}
	/**
	 * Retorna o usuario que solicitou a amizade
	 * @return login do usuario requisitante
	 */

	public String getRequestingUser() {
		return requestingUser;
	}
	
	/**
	 * Altera o usuario requisitante
	 * @param loginUser
	 */

	public void setRequestingUser(String loginUser) {
		this.requestingUser = loginUser;
	}
	
	/**
	 * Retorna o usuario alvo da requisicao de amizade
	 * @return login do usuario alvo
	 */

	public String getDestinationUser() {
		return destinationUser;
	}
	
	/**
	 * Altera o usuario alvo da requisicao
	 * @param login do usuario alvo
	 */

	public void setDestinationUser(String inUser) {
		this.destinationUser = inUser;
	}
	
	private void generateCode(){
		int cont;
		int num;
		do{
			cont = 0;
			List<Integer> codeList = System.getInstance().getRequestCodeList();
			Random rand = new Random();
			num = rand.nextInt();
			if(num < 0) num *= -1;
			if(codeList.size() == 0){
				System.getInstance().addRequestCodeList(num);
			}else{
				for(int i = 0; i < codeList.size(); i++){
					if(codeList.get(i) == num){
						++cont;
						break;
					}
				}
		}
		
		}while(cont != 0);
		
		System.getInstance().addRequestCodeList(num);
		codeRequest =  num;
		
	}
	
	/**
	 * Retorna o codigo da requisicao
	 * @return
	 */
	public int getRequestCode(){
		return codeRequest;
	}
	
	/**
	 * Verifica se duas Requisicoes sao iguais
	 */
	@Override
	public boolean equals(Object object){
		if(!(object instanceof Request)) return false;
		
		Request request = (Request) object;
		if(requestingUser.equals(request.getRequestingUser()) && destinationUser.equals(request.getDestinationUser()) 
				&& (getRequestCode() == request.getRequestCode())) return true;
		else return false;
	}
	
}
